﻿using System;

using Datagility.Shpt.SPDocMirror.Settings;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

namespace Datagility.Shpt.SPDocMirror.UI.Layouts.Datagility.Shpt.SPDocMirror.UI
{
    public partial class MirrorMainSettingsPage : LayoutsPageBase
    {
        private SPSite siteCollection;
        private MirrorJobSettings settings;

        private const string REDIRECT_URL = @"/_layouts/settings.aspx";

        protected void Page_Load(object sender, EventArgs e)
        {
            //Get the stored settings
            siteCollection = SPContext.Current.Web.Site;
            settings = MirrorJobSettingsFactory.GetMirrorJobSettings(siteCollection.Url);

            if (!this.IsPostBack)
            {
                //Set control values based on the read settings
                txtRootLocation.Text = settings.ShadowRoot;
            }
        }

        protected override void OnInit(EventArgs e)
        {
            BtnUpdateMirroringSettings.Click += new EventHandler(BtnUpdateMirroringSettings_Click);
        }

        void BtnUpdateMirroringSettings_Click(object sender, EventArgs e)
        {
            settings.ShadowRoot = txtRootLocation.Text;

            MirrorJobSettingsFactory.SetMirrorJobSettings(siteCollection.Url, settings);

            SPUtility.Redirect(REDIRECT_URL, SPRedirectFlags.Default,
                this.Context);
        }

        public override string PageToRedirectOnCancel
        {
            get
            {
                return REDIRECT_URL;
            }
        }
    }
}
